package com.yztstudy.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yztstudy.Mapper.CollectMapper;
import com.yztstudy.Service.CollectService;
import com.yztstudy.clients.ProductFeignClient;
import com.yztstudy.entity.Collect;
import com.yztstudy.param.ProductCollectParam;
import com.yztstudy.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class CollectServiceImpl implements CollectService {

    @Autowired
    private CollectMapper collectMapper;

    @Resource
    private ProductFeignClient productFeignClient;

    /**
     * TODO 保存收藏
     * @param collect 收藏品
     * @return
     */
    @Transactional
    @Override
    public R save(Collect collect) {

        // TODO 1.先查询数据是否存在
        QueryWrapper<Collect> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", collect.getUserId()); // 某个具体的用户
        wrapper.eq("product_id", collect.getProductId()); // 收藏的某个具体的商品
        Long count = collectMapper.selectCount(wrapper);

        // TODO 2.如果存在
        if (count > 0)
        {
            log.info("CollectServiceImpl.save业务结束, 结果:{}", "收藏已经添加,无需二次添加!");
            return R.fail("收藏已经添加,无需二次添加!");
        }

        // TODO 3.如果不存在，就添加
        else {
            collect.setCollectTime(System.currentTimeMillis());
            collectMapper.insert(collect);
            log.info("CollectServiceImpl.save业务结束, 结果:{}", "收藏添加成功!");
            return R.ok("收藏添加成功!");
        }
    }


    /**
     * TODO 根据用户id查询他收藏的商品的id集合
     * @param collect 保存了用户ID
     * @return
     */
    @Override
    public R list(Collect collect) {

        // TODO 1.先查询所有收藏的商品ID
        QueryWrapper<Collect> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", collect.getUserId());
        wrapper.select("product_id");
        List<Object> objects = collectMapper.selectObjs(wrapper);

        // TODO 2.Object类型转换为Integer类型
        List<Integer> productIds = new ArrayList<>();
        for (Object object : objects)
        {
            Integer num = (Integer) object;
            productIds.add(num);
        }

        // TODO 3.查询所有收藏的商品信息
        ProductCollectParam productCollectParam = new ProductCollectParam();
        productCollectParam.setProductIds(productIds);
        R result = productFeignClient.productIds(productCollectParam);

        // TODO 4.返回结果给前端
        log.info("CollectServiceImpl.list业务结束, 结果:{}", "收藏商品查询成功!");
        return result;
    }


    /**
     * TODO 删除收藏
     * @param collect 包含了用户ID和商品ID
     * @return
     */
    @Transactional // 写操作，用事务
    @Override
    public R remove(Collect collect) {

        // TODO 1.封装条件
        QueryWrapper<Collect> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", collect.getUserId());
        wrapper.eq("product_id", collect.getProductId());

        // TODO 2.删除
        int rows = collectMapper.delete(wrapper);

        // TODO 3.返回结果
        if (rows == 0)
        {
            log.info("CollectServiceImpl.list业务结束, 结果:{}", "删除收藏品失败!");
            return R.fail("删除收藏品失败!");
        }else {
            log.info("CollectServiceImpl.list业务结束, 结果:{}", "删除收藏品成功!");
            return R.ok("删除收藏品成功!");
        }
    }


    /**
     * TODO 删除收藏里面的商品
     * @param productId 商品Id
     * @return
     */
    @Transactional
    @Override
    public R removeByProductId(Integer productId) {

        // TODO 1.构造删除条件
        QueryWrapper<Collect> wrapper = new QueryWrapper<>();
        wrapper.eq("product_id", productId);
        collectMapper.delete(wrapper);

        // TODO 2.返回结果
        return R.ok("删除收藏成功!");
    }
}
